package com.xy.sql.xml.node.impl;


import com.xy.sql.xml.core.XySQLExePara;
import com.xy.sql.xml.core.XySQLTaskContext;
import com.xy.sql.xml.itf.XySQLRegister;
import com.xy.sql.xml.node.XySQLSuperNode;

import java.util.ArrayList;
import java.util.List;

public class XySQLIncludeNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "include";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLIncludeNode();
    }


    /**
     * 构造SQL，绑定参数
     *
     * @param task
     * @return
     * @throws Exception
     */
    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        List<XySQLExePara> resList = new ArrayList<>();
        String refid = this.getPropertyMap().get("refId");
        if (refid == null || refid.length() == 0) {
            refid = this.getPropertyMap().get("refid");
        }
        if (refid != null && refid.length() > 0) {
            XySQLSuperNode node = task.getSqlFragmentById(refid);
            if (node == null) {
                XySQLRegister.getLogger().debug("refId(" + refid + ")对应的SQL片段不存在");
                return resList;
            }
            resList.addAll(node.boundSql(task));
        } else {
            XySQLRegister.getLogger().debug("refId(" + refid + "), 属性不存在");
        }
        return resList;
    }
}
